package ro.msg.quiz.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//AUTHORS: Ervin, Marco

public class DatabaseCon {
    private static final String databaseName = "smart_quest";
    private static final String url = "jdbc:mysql://localhost:3306/" + databaseName;
    private static final String userName = "root";
    private static final String password = "1234";
    private static final String driver = "com.mysql.jdbc.Driver";

    private Connection con;
    private static DatabaseCon instance;

    public DatabaseCon() throws ClassNotFoundException, SQLException {
	Class.forName(driver);
	con = DriverManager.getConnection(url, userName, password);
    }

    public void executeUpdate(String updateString) throws SQLException {
	Statement stm = con.createStatement();
	stm.executeUpdate(updateString);
    }

    public void executeBatch(String s[]) throws SQLException {
	Statement stm = con.createStatement();
	for (int i = 0; i < s.length; i++)
	    stm.addBatch(s[i]);

	stm.executeBatch();
    }

    public ResultSet executeQuery(String query) throws SQLException {
	Statement stm = con.createStatement();
	return stm.executeQuery(query);
    }

    public static DatabaseCon getInstance() throws ClassNotFoundException, SQLException {
	if (instance != null)
	    return instance;
	instance = new DatabaseCon();
	return instance;
    }

    public Connection getConnection() {
	return con;
    }
}
